package com.learn.xueer.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseActivity1
import com.learn.xueer.app.ext.init
import com.learn.xueer.data.model.bean.BookInfo
import com.learn.xueer.data.model.bean.HearSayResponse
import com.learn.xueer.databinding.ActivityBookInfoBinding
import com.learn.xueer.ui.adapter.BookSectionAdapter
import com.learn.xueer.ui.viewmodel.RequestHearViewModel
import kotlinx.android.synthetic.main.activity_book_info.recyclerView
import kotlinx.android.synthetic.main.activity_book_info.smartRefresh
import me.hgj.jetpackmvvm.ext.parseState

class BookInfoActivity : BaseActivity1<RequestHearViewModel, ActivityBookInfoBinding>() {

    private lateinit var bookInfo: HearSayResponse
    private val sectionAdapter: BookSectionAdapter by lazy { BookSectionAdapter(arrayListOf()) }
    private var order = "asc"

    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(mViewModel)
        mViewBind.viewmodel = mViewModel
        mViewBind.click = ProxyClick()
        bookInfo = intent.extras?.getParcelable<HearSayResponse>("bookInfo")!!
        bindData()

        mViewModel.bookSectionReq(bookInfo.productId.toString(), order)
        recyclerView.init(LinearLayoutManager(this), sectionAdapter)

        smartRefresh.setEnableLoadMore(false)
        smartRefresh.setOnRefreshListener {
            loadSectionData()
        }

        sectionAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this,SectionDetailActivity::class.java)
            intent.putExtra("id",sectionAdapter.data[position].id)
            intent.putExtra("pid",sectionAdapter.data[position].productId)
            intent.putExtra("title",sectionAdapter.data[position].name)
            startActivity(intent)
        }

        loadSectionData()
    }

    private fun loadSectionData(){
        mViewModel.bookSectionReq(bookInfo.productId.toString(), order)
    }

    private fun bindData() {
        val bookInfos = Gson().fromJson<MutableList<BookInfo>>(
            bookInfo.packageinfo,
            object : TypeToken<MutableList<BookInfo>>() {}.type
        )
        var bookSection = bookInfos.find {
            it.title == "章节总数"
        }
        mViewModel.bookInfoName.set(bookInfo.productName + "：" + bookSection?.content)
        mViewModel.bookInfoDesc.set(bookInfos.first().title + "：" + bookInfo.gradeName)

        val sectionCount =
            bookSection?.content?.removeRange(
                bookSection.content.length - 1,
                bookSection.content.length
            )
        mViewModel.bookInfoTotalSection.set(sectionCount)
    }

    override fun createObserver() {
        mViewModel.bookSectionResult.observe(this, Observer { resultState ->
            parseState(resultState, {
                mViewModel.bookInfoCurrentSection.set(it?.size.toString())
                sectionAdapter.setList(it)
                if (smartRefresh.isRefreshing){
                    smartRefresh.finishRefresh()
                }
            }, {})
        })
    }

    inner class ProxyClick {
        fun returnBack() {
            finish()
        }

        fun order(){
            order = if (order == "asc") "desc" else "asc"
            mViewModel.drawableUrl.set(if (order == "desc") R.mipmap.icon_desc else R.mipmap.icon_asc)
            loadSectionData()
        }
    }
}